using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Isis
{
    public class ApplicationFolder
    {
        public IList<Assembly> ListRunningAssemblies()
        {
            var myList = new List<Assembly>();
            var executingAssembly = Assembly.GetExecutingAssembly();

            var assemblyPath = GetAssemblyFolder(executingAssembly);
            var files = ListFilesInFolder(assemblyPath, "*.dll");

            foreach (var file in files)
            {
                var myAssembly = Assembly.LoadFile(Path.GetFullPath(file.FullName));
                myList.Add(myAssembly);
            }

            return myList;
        }

        public string GetAssemblyFolder(Assembly executingAssembly)
        {
            return Path.GetDirectoryName(executingAssembly.CodeBase).Remove(0, 6);
        }

        public string GetAssemblyFolder()
        {
            return GetAssemblyFolder(Assembly.GetExecutingAssembly());
        }

        public FileInfo[] ListFilesInFolder(string path, string filter)
        {
            var dirInfo = new DirectoryInfo(path);
            FileInfo[] files = dirInfo.GetFiles(filter);
            return files;
        }

        public FileInfo[] ListFilesInFolder()
        {
            return ListFilesInFolder(GetAssemblyFolder(), "*.*");
        }
    }
}